package dp

func minInt(a, b int) int {
	if a < b {
		return a
	}
	return b
}

// https://leetcode-cn.com/problems/min-cost-climbing-stairs/submissions/
// 最小花费爬楼梯
// 算法：动态规划
// f(n) = cost(n) + min(f(n -1), f(n - 2))
func MinCostClimbingStairs(cost []int) int {
	if 0 == len(cost) {
		return 0
	} else if 1 == len(cost) {
		return 0
	}

	currentCostArr := make([]int, len(cost))
	currentCostArr[0] = cost[0]
	currentCostArr[1] = cost[1]

	for index := 2; index < len(cost); index++ {
		currentCostArr[index] = minInt(currentCostArr[index-1], currentCostArr[index-2]) + cost[index]
	}

	return minInt(currentCostArr[len(cost)-1], currentCostArr[len(cost)-2])
}
